package com.bn.droplet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;

import atg.commerce.order.CommerceItem;
import atg.commerce.order.ShippingGroup;
import atg.commerce.pricing.PricingAdjustment;
import atg.repository.RepositoryItem;
import atg.servlet.DynamoHttpServletRequest;
import atg.servlet.DynamoHttpServletResponse;
import atg.servlet.DynamoServlet;
import com.bn.commerce.order.BNOrderImpl;

/**
 * @author Amplifi
 * Class for Dipalying the applied promotion
 *
 */
public class BNPromotionDisplayDroplet  extends DynamoServlet{




	private static final String PARAM_ORDER = "order";
	private static final String PARAM_COMMERCE_ITEM = "commerceItem";

	private static final String OPARAM_OUTPUT = "output";
	private static final String OPARAM_OUTPUT_START = "outputStart";

	private static final String OUTPUT_PARAM_COUPON_CODE = "couponCode";
	private static final String OUTPUT_PARAM_ADJUSTMENT = "adjustment";

	private BNOrderImpl order;

	private boolean isOutputStartRendered;

	@Override
	public void doGet(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);

	}


	@Override
	public void doPost(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
			throws ServletException, IOException {

		displayPromotions(request, response);

	}


	/**
	 * Display the promotions for the order.
	 *
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void displayPromotions(DynamoHttpServletRequest request, DynamoHttpServletResponse response)
			throws ServletException, IOException {

		isOutputStartRendered = false;

		BNOrderImpl order = (BNOrderImpl) request.getObjectParameter(PARAM_ORDER);
		if (order == null) {
			order = getOrder();
		}

		//Display item promotions
		//display only the passed in item promotion if the commerce item is given
			CommerceItem commerceItem = (CommerceItem) request.getObjectParameter(PARAM_COMMERCE_ITEM);
			if (commerceItem != null) {
				if (commerceItem.getPriceInfo() != null) {
					displayPromotionAdjustments(order, commerceItem.getPriceInfo().getAdjustments(), request, response);
					return;
				}
			} else {
				//display all of the item promotions if no commerce item parameter is passed
				for (Object commerceItemCurr : order.getCommerceItems()) {
					if (((CommerceItem) commerceItemCurr).getPriceInfo() != null) {
						displayPromotionAdjustments(order, ((CommerceItem) commerceItemCurr).getPriceInfo().getAdjustments(), request, response);
					}
				}
			}



		//Display shipping promotions
			List shippingGroups = order.getShippingGroups();
			for (Object shipping : shippingGroups) {

				if (shipping != null && ((ShippingGroup) shipping).getPriceInfo() != null) {
					List adjustments = ((ShippingGroup) shipping).getPriceInfo().getAdjustments();
					displayPromotionAdjustments(order, adjustments, request, response);
				}
			}



		//Display order promotions

			if (order.getPriceInfo() != null && order.getPriceInfo().getDiscountAmount() > 0) {
				displayPromotionAdjustments(order, order.getPriceInfo().getAdjustments(), request, response);
			}

		}




	/**
	 * This method sets the coupon code and the adjustment on the parameter and
	 * renders the output parameter.
	 *
	 * @param order
	 * @param adjustments
	 * @param request
	 * @param response
	 *
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void displayPromotionAdjustments(BNOrderImpl order, List adjustments, DynamoHttpServletRequest request, DynamoHttpServletResponse response)
			throws ServletException, IOException {

		if (adjustments != null && adjustments.size() > 0) {
			for (Object adjustmentObj : adjustments) {
				PricingAdjustment adjustment = (PricingAdjustment) adjustmentObj;
				RepositoryItem pricingModel = adjustment.getPricingModel();

				if (pricingModel != null) {

					//If there is a promotion and the output start hasn't been rendered yet, render the output start param
					if (!isOutputStartRendered) {
						isOutputStartRendered = true;
						request.serviceParameter(OPARAM_OUTPUT_START, request, response);
					}

					//Set the parameters - the coupon code and the adjustment to be displayed
					//TODO couponCode - if this is an infinite use coupon/promtion, the promotion code will not show up. Look for a place where the promotion is redeemed automatically
					/*String couponCode = order.getCouponCode(pricingModel.getRepositoryId());
					request.setParameter(OUTPUT_PARAM_COUPON_CODE, couponCode);*/
					request.setParameter(OUTPUT_PARAM_ADJUSTMENT, adjustment);

					request.serviceParameter(OPARAM_OUTPUT, request, response);
				}
			}
		}

	}



	public BNOrderImpl getOrder() {

		return order;
	}



	public void setOrder(BNOrderImpl order) {

		this.order = order;
	}


	/**
	 * @return the order
	 */

}